import { http } from '../utils/request.js'

// 获取用户详情
export const getUserInfo = (userId) => {
  return http.get(`/users/${userId}`)
}

// 更新用户信息
export const updateUser = (userId, data) => {
  return http.put(`/users/${userId}`, {
    userName: data.userName,
    email: data.email,
    phoneNumber: data.phoneNumber
  })
}

// 修改密码
export const changePassword = (userId, data) => {
  return http.post(`/users/${userId}/change-password`, {
    oldPassword: data.oldPassword,
    newPassword: data.newPassword,
    confirmPassword: data.confirmPassword
  })
}

// 上传头像
export const uploadAvatar = (userId, file) => {
  const formData = new FormData()
  formData.append('file', file)
  
  return http.post(`/users/${userId}/avatar`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取用户头像信息（文件名/URL/大小等）
export const getAvatarInfo = (userId) => {
  return http.get(`/users/${userId}/avatar-info`)
}

// 预览头像文件（返回Blob）
export const getAvatarFile = (fileName, config = {}) => {
  return http.get(`/users/avatar-file/${encodeURIComponent(fileName)}`, {}, { responseType: 'blob', ...config })
}

// 获取用户等级
export const getUserLevel = (userId) => {
  return http.get(`/userlevel/${userId}`)
}

// 获取用户称号
export const getUserTitles = (userId) => {
  return http.get(`/userlevel/${userId}/titles`)
}

// 获取所有可用称号
export const getAllTitles = () => {
  return http.get('/userlevel/all-titles')
}

// 统一导出用户API
export const userAPI = {
  getUserInfo,
  updateUser,
  changePassword,
  uploadAvatar,
  getAvatarInfo,
  getAvatarFile,
  getUserLevel,
  getUserTitles,
  getAllTitles
}
